package com.nuc.bigdata.controller;

import com.nuc.bigdata.orm.entity.Picture;
import com.nuc.bigdata.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/14
 */
@Api(value = "图床Controller")
@RestController
@RequestMapping("/oa-picture")
public class PictureController {

    private final PictureService pictureService;

    @Autowired
    PictureController(PictureService pictureService) {
        this.pictureService = pictureService;
    }

    /**
     * 上传单个文件
     *
     * @param file   MultipartFile 文件类型
     * @param userId 用户唯一标识
     * @return 插入图片的实体信息
     */
    @ApiOperation(value = "上传单个图片文件",notes = "选择本地文件进行上传",tags = {"文件上传接口"})
    @PostMapping("/{userId}/single")
    public Picture uploadPicture(@ApiParam(value = "图片文件",name = "picture") @RequestParam("picture") MultipartFile file
            , @ApiParam(value = "用户唯一标识",name = "userId")@PathVariable("userId") String userId) {
        return pictureService.uploadPicture(file, userId);
    }

    @ApiOperation(value = "获取指定图片文件信息",notes = "图片实体信息展示",tags = {"获取图片信息接口"})
    @GetMapping("/{pictureId}")
    public Picture getPicture(@ApiParam(value = "图片唯一标识",name = "pictureId")@PathVariable("pictureId") String pictureId) {
        return pictureService.getPicture(pictureId);
    }

    @ApiOperation(value = "获取指定图片地址",notes = "获取图片完整大小的url",tags = {"获取图片地址接口"})
    @GetMapping("/url/{pictureId}")
    public String getPictureUrl(@ApiParam(value = "图片唯一标识",name = "pictureId")@PathVariable("pictureId") String pictureId) {
        Picture picture = pictureService.getPicture(pictureId);
        return pictureService.getPictureUrl(picture);
    }

    @ApiOperation(value = "获取指定图片地址",notes = "获取图片缩略大小的url",tags = {"获取图片地址接口"})
    @GetMapping("/thumbnailUrl/{pictureId}")
    public String getThumbnailUrl(@ApiParam(value = "图片唯一标识",name = "pictureId")@PathVariable("pictureId") String pictureId) {
        Picture picture = pictureService.getPicture(pictureId);
        return pictureService.getThumbnailUrl(picture);
    }


    @ApiOperation(value = "上传多个图片文件",notes = "选择本地文件进行上传",tags = {"文件上传接口"})
    @PostMapping("/{userId}/many")
    public ArrayList<Picture> uploadManyPicture(@ApiParam(value = "多个图片文件",name = "pictures")@RequestParam("pictures") ArrayList<MultipartFile> files,
                                                @ApiParam(value = "用户唯一标识",name = "userId") @PathVariable("userId") String userId) {
        return pictureService.uploadManyPicture(files, userId);
    }
}
